package com.yanqu.road.server.cmd.activity.jinli.cross;

import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.JinLiProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.MallRechargeConfigMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.MessageFormat;

@Cmd(code = Protocol.S_JINLI_SEND_REWARD_FROM_CROSS, desc = "锦鲤跨服发奖励来")
public class JinLiSendRewardFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        JinLiProto.CrossSendJinLiRewardReqMsg respMsg = JinLiProto.CrossSendJinLiRewardReqMsg.parseFrom(packet.getBytes());
        // 发邮件
        String language = UserMgr.getLanguage(respMsg.getUserId());
        String mailTitle = MultipleLanguageMgr.getContent(MailManager.LUCKYSHOP_MALL_TITLE, language);
        // 获取礼包档位
        MallInfo mallInfo = MallMgr.getMallInfoByMallId(respMsg.getMallId());
        // 算下价格
        long price = MallRechargeConfigMgr.getPrice(mallInfo);
        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.LUCKYSHOP_MALL_CONTENT, language),
                BigDecimal.valueOf(price).divide(BigDecimal.valueOf(100), 8, RoundingMode.UP).stripTrailingZeros());
        MailManager.sendMail(respMsg.getUserId(), eMailType.SYSTEM.getValue(), respMsg.getReward(), mailContent, mailTitle);
    }
}
